package jude.java.util;

import jude.java.money.black.RegularExpression;

import org.apache.oro.text.regex.*;

public class RegexUtils {
	
	public static String replaceAll(String str, String regex, String to) {
		return replaceAll(str, regex, to, 0, 0);
	}

	public static String replaceAll(String str, String regex, String to, int timeGroupIndex, long time) {
	    Pattern pattern   = null;
	    PatternCompiler compiler = new Perl5Compiler();
	    PatternMatcher matcher  = new Perl5Matcher();
	    int mask = Perl5Compiler.CASE_INSENSITIVE_MASK;

		try {
			pattern = compiler.compile(regex, mask);
		} catch (MalformedPatternException e) {
			System.err.println("Bad pattern: " + regex);
			System.err.println(e.getMessage());
			return str;
		}
    	int start = 0;
    	int count = 0;
    	StringBuilder sb = new StringBuilder(4096);
    	PatternMatcherInput input = new PatternMatcherInput(str);
	    MatchResult result;
		while (matcher.contains(input, pattern)) {
			count ++;
	    	result = matcher.getMatch();
	    	if (timeGroupIndex > 0 && time > 0 && result.groups() >= timeGroupIndex) {
	    		String t = result.group(timeGroupIndex);
	    		long tt = Utils.getLong(t, 0L);
	    		if (tt > time) {
	    			return null;	//exists
	    		}
	    	}
    		sb.append(str.substring(start, result.beginOffset(0)));
    		if (to != null)
    			sb.append(to);
    		start = result.endOffset(0);
		}
		if (count > 0) {
			sb.append(str.substring(start));
		} else {
			return str;
		}
	    return sb.toString();
	}
	
	public static String replaceAll(String str, RegularExpression regex, String to, int timeGroupIndex, long time) {
    	int startLen = regex.getStart().length();
    	int endLen = regex.getEnd().length();
    	int start = str.indexOf(regex.getStart());
    	if (start == -1)
    		return str;
    	int end = str.indexOf(regex.getEnd(), start + startLen);
    	if (end == -1)
    		return str;
	    Pattern pattern = null;
		try {
			pattern = new Perl5Compiler().compile(regex.getRegexp(), Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.SINGLELINE_MASK);
//			System.out.println(regex.getRegexp());
		} catch (MalformedPatternException e) {
			System.err.println("Bad pattern: " + regex.getRegexp());
			System.err.println(e.getMessage());
			return str;
		}
    	int count = 0, last = 0;
    	StringBuilder sb = new StringBuilder(8192);
	    PatternMatcher matcher  = new Perl5Matcher();
	    MatchResult result;
    	do {
    		String content = str.substring(start + startLen, end);
    		int k = content.indexOf(regex.getStart());
    		if (k != -1) {
    			start += startLen + k;
    			continue;
    		}
    		if (Utils.inScript(str, start)) {
    			k = str.indexOf("</script>", start);
    			start = str.indexOf(regex.getStart(), k + 9);
            	if (start != -1) {
            		end = str.indexOf(regex.getEnd(), start + startLen);
            	}
    			continue;
    		}
        	PatternMatcherInput input = new PatternMatcherInput(content);
    		if (matcher.matches(input, pattern)) {
    			result = matcher.getMatch();
    	    	if (timeGroupIndex > 0 && time > 0) {
    	    		if (result.groups() >= timeGroupIndex) {
        	    		String t = result.group(timeGroupIndex);
        	    		long tt = Utils.getLong(t, 0L);
        	    		if (tt > time)
        	    			return null;	//exists
        	    		sb.append(str.substring(last, start));
        	    		count ++;
    	    		} else {
        	    		sb.append(str.substring(last, end + endLen));
    	    		}
    	    	} else {
    	    		count ++;
    	    		sb.append(str.substring(last, start));
    	    	}
        		last = end + endLen;
    		}
        	start = str.indexOf(regex.getStart(), end + endLen);
        	if (start != -1) {
        		end = str.indexOf(regex.getEnd(), start + startLen);
        	}
    	} while (start != -1 && end != -1);
		if (count > 0) {
			sb.append(str.substring(last));
		} else {
			return str;
		}
	    return sb.toString();
	}

}
